package com.ruoyi.comparison.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.comparison.domain.TaskJudgeDetail;
import com.ruoyi.comparison.domain.bo.TaskJudgeDetailBo;
import com.ruoyi.comparison.domain.vo.TaskJudgeDetailVo;
import com.ruoyi.comparison.mapper.TaskJudgeDetailMapper;
import com.ruoyi.comparison.service.ITaskJudgeDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 当事人信息，关联裁判文书信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-18
 */
@RequiredArgsConstructor
@Service
public class TaskJudgeDetailServiceImpl implements ITaskJudgeDetailService {

    private final TaskJudgeDetailMapper baseMapper;

    /**
     * 查询当事人信息，关联裁判文书信息
     */
    @Override
    public TaskJudgeDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询当事人信息，关联裁判文书信息列表
     */
    @Override
    public TableDataInfo<TaskJudgeDetailVo> queryPageList(TaskJudgeDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TaskJudgeDetail> lqw = buildQueryWrapper(bo);
        lqw.eq(TaskJudgeDetail::getJudgeId,bo.getJudgeId());
        Page<TaskJudgeDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询当事人信息，关联裁判文书信息列表
     */
    @Override
    public List<TaskJudgeDetailVo> queryList(TaskJudgeDetailBo bo) {
        LambdaQueryWrapper<TaskJudgeDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TaskJudgeDetail> buildQueryWrapper(TaskJudgeDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TaskJudgeDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getJudgeId() != null, TaskJudgeDetail::getJudgeId, bo.getJudgeId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), TaskJudgeDetail::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getRoleType()), TaskJudgeDetail::getRoleType, bo.getRoleType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatement()), TaskJudgeDetail::getStatement, bo.getStatement());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyno()), TaskJudgeDetail::getKeyno, bo.getKeyno());
        return lqw;
    }

    /**
     * 新增当事人信息，关联裁判文书信息
     */
    @Override
    public Boolean insertByBo(TaskJudgeDetailBo bo) {
        TaskJudgeDetail add = BeanUtil.toBean(bo, TaskJudgeDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改当事人信息，关联裁判文书信息
     */
    @Override
    public Boolean updateByBo(TaskJudgeDetailBo bo) {
        TaskJudgeDetail update = BeanUtil.toBean(bo, TaskJudgeDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TaskJudgeDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除当事人信息，关联裁判文书信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<TaskJudgeDetailVo> findByJudgeId(Long id) {
        LambdaQueryWrapper<TaskJudgeDetail> w = new LambdaQueryWrapper<>();
        w.eq(TaskJudgeDetail::getJudgeId,id);
        List<TaskJudgeDetailVo> taskJudgeDetailVos = this.baseMapper.selectVoList(w);
        return taskJudgeDetailVos;
    }

    @Override
    public void insertBatch(List<TaskJudgeDetail> taskJudgeDetails) {
        this.baseMapper.insertBatch(taskJudgeDetails);
    }
}
